﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

namespace Common
{
    /// <summary>
    /// 错误详情
    /// </summary>
    [DebuggerDisplay("ResultType:{Message}")]
    public class PromptInfo : IPromptInfo
    {
        #region Property
        private string _customMessageStack;
        private string _messageStack;

        /// <inheritdoc/>
        public string CustomMessage { get; private set; }

        /// <inheritdoc/>
        public string CustomMessageStack
        {
            get
            {
                if (string.IsNullOrEmpty(this._customMessageStack))
                {
                    this.LoadResultStack(this, 1);
                }
                return this._customMessageStack;
            }
        }

        /// <inheritdoc/>
        public IPromptInfo InnerPromptInfo { get; private set; }

        /// <inheritdoc/>
        public string Message { get; private set; }

        /// <inheritdoc/>
        public string MessageStack
        {
            get
            {
                if (string.IsNullOrEmpty(this._messageStack))
                {
                    this.LoadResultStack(this, 1);
                }
                return this._messageStack;
            }
        }
        /// <inheritdoc/>
        public ResultType ResultType { get; set; }

        /// <inheritdoc/>
        public int Code { get { return (int)ResultType; } }

        #endregion

        /// <inheritdoc/>
        public void Alert(ResultType restulType)
        {
            this.ResultType = restulType;
            this.Message = string.Format("{{错误代码:{0} {1}}}", (int)restulType, restulType);
        }

        /// <inheritdoc/>
        public void Alert(string message)
        {
            this.CustomMessage = message;
            ResultType = ResultType.服务器繁忙;
            this.Message = message + @"  " + string.Format("{{错误代码:{0} {1}}}", (int)ResultType, ResultType);
        }

        /// <inheritdoc/>
        public void Alert(ResultType restulType, IPromptInfo innerPromptInfo)
        {
            if (innerPromptInfo == this)
                return;
            this.InnerPromptInfo = innerPromptInfo;
            this.Alert(restulType);
        }

        /// <inheritdoc/>
        public void Alert(ResultType restulType, string message)
        {
            this.CustomMessage = message;
            this.ResultType = restulType;
            this.Message = message + @"  " + string.Format("{{错误代码:{0} {1}}}", (int)ResultType, ResultType);
        }

        /// <inheritdoc/>
        public void Alert(string message, IPromptInfo innerPromptInfo)
        {
            if (innerPromptInfo == this)
                return;
            this.InnerPromptInfo = innerPromptInfo;
            this.Alert(message);
        }
        /// <inheritdoc/>
        public void Alert(ResultType restulType, string message, IPromptInfo innerPromptInfo)
        {
            if (innerPromptInfo != this)
            {
                this.InnerPromptInfo = innerPromptInfo;
            }
            this.Alert(restulType, message);
        }

        private void LoadResultStack(IPromptInfo promptInfo, int index)
        {
            if (index == 1)
            {
                this._customMessageStack = string.Empty;
                this._messageStack = string.Empty;
            }
            string str1 = this._customMessageStack;
            if (!string.IsNullOrEmpty(promptInfo.CustomMessage))
            {
                this._customMessageStack = string.Concat(new object[] { str1, index > 1 ? @";" : string.Empty, index, ".", promptInfo.CustomMessage });
            }
            string str2 = this._messageStack;
            if (!string.IsNullOrEmpty(promptInfo.Message))
            {
                this._messageStack = string.Concat(new object[] { str2, index > 1 ? @";" : string.Empty, index, ".", promptInfo.Message });
            }
            if (promptInfo.InnerPromptInfo != null)
            {
                this.LoadResultStack(promptInfo.InnerPromptInfo, ++index);
            }
        }

        /// <inheritdoc/>
        public FuncResult AsFailResult(string defaultMessage = null)
        {
            return this.ToFailResult(defaultMessage);
        }
        /// <inheritdoc/>
        public FuncResult<T> AsFailResult<T>(string defaultMessage = null)
        {
            return this.ToFailResult<T>(defaultMessage);
        }
    }
}
